<?php
/*
Plugin Name: 0 GRAND Flash Media
Plugin URI: http://codeasily.com/wordpress-plugins/grand-flash-media/
Description: The GRAND FlaM plugin - provides a comprehensive interface for managing photos and images through a set of admin pages, and it displays photos in a way that makes your web site look very professional.
Version: 0.6.0
Author: Sergey Pasyuk
Author URI: http://codeasily.com/

-------------------

		Copyright 2011  Sergey Pasyuk  (email : pasyuk@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

// Stop direct call
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }

ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
if (!class_exists('grandLoad')) {
class grandLoad {
	
	var $version     = '0.6.0';
	var $dbversion   = '0.6.0';
	var $minium_WP   = '3.0';
	var $options     = '';
	var $library;
	var $core;
	var $gMDb;
	
	function grandLoad() {

		// Stop the plugin if we missed the requirements
		if ( !$this->required_version() )
			return;
			
		// Get some constants first
		$this->load_options();
		$this->define_constant();
		$this->load_dependencies();
		
		$this->plugin_name = plugin_basename(__FILE__);

		// Init options & tables during activation & deregister init option
		register_activation_hook( $this->plugin_name, array(&$this, 'activate') );
		register_deactivation_hook( $this->plugin_name, array(&$this, 'deactivate') );	

		// Register a uninstall hook to remove all tables & option automatic
		register_uninstall_hook( $this->plugin_name, array('grandLoader', 'uninstall') );

		// Start this plugin once all other plugins are fully loaded
		add_action( 'plugins_loaded', array(&$this, 'start_plugin') );
		
		//Add some message on the plugin page
		//add_action( 'after_plugin_row', array(&$this, 'check_message_version') );

		//Add some links on the plugin page
		add_filter('plugin_row_meta', array(&$this, 'add_plugin_links'), 10, 2);
        
	}
	
	function start_plugin() {

		// Load the language file
		$this->load_textdomain();
		
        // Check for upgrade
        $this->upgrade();
				
		// Load the admin panel or the frontend functions
		if ( is_admin() ) {	
			
			// Pass the init check or show a message
			if (get_option( "grand_init_check" ) != false )
				add_action( 'admin_notices', create_function('', 'echo \'<div id="message" class="error"><p><strong>' . get_option( "grand_init_check" ) . '</strong></p></div>\';') );
				
		} else {			
			
			// Add the script and style files
			add_action('template_redirect', array(&$this, 'load_scripts') );
			add_action('template_redirect', array(&$this, 'load_styles') );

			// Add a version number to the header
			add_action('wp_head', create_function('', 'echo "\n<meta name=\'GrandMedia\' content=\'' . $this->version . '\' />\n";') );

		}	
	}
	
	function required_version() {
		
		global $wp_version;
			
		// Check for WP version installation
		$wp_ok  =  version_compare($wp_version, $this->minium_WP, '>=');
		
		if ( ($wp_ok == FALSE) ) {
			add_action( 'admin_notices', create_function( '', 'global $grandLoad; printf (\'<div id="message" class="error"><p><strong>\' . __(\'Sorry, GrandMedia works only under WordPress %s or higher\', "gmLang" ) . \'</strong></p></div>\', $grandLoad->minium_WP );' ) );
			return false;
		}
		
		return true;
	}
	
	function upgrade() {

		// Inform about a database upgrade
		if( get_option( 'grand_db_version' ) != GRAND_DBVERSION ) {
			add_action(	'admin_notices', create_function( '', 'echo \'<div id="message" class="error"><p><strong>' . __('Please update the database of GRAND Flash Media.', 'gmLang') . ' <a href="admin.php?page=grand-media">' . __('Click here to proceed.', 'gmLang') . '</a>' . '</strong></p></div>\';' ) );
		}
        
		return;		
	}
	
	function define_constant() {
		
		define('GRAND_VERSION', $this->version);
		// Minimum required database version
		define('GRAND_DBVERSION', $this->dbversion);

		// define plugin pathes
		define('GRAND_FOLDER', plugin_basename( dirname(__FILE__)) );
		define('GRAND_ABSPATH', str_replace("\\","/", WP_PLUGIN_DIR.'/'.GRAND_FOLDER.'/'));
		define('GRAND_URLPATH', WP_PLUGIN_URL.'/'.GRAND_FOLDER.'/');
		define('GRAND_MEDIA_ABS', str_replace("\\","/", WP_CONTENT_DIR.'/'.GRAND_FOLDER.'/'));
		define('GRAND_MEDIA_URL', WP_CONTENT_URL.'/'.GRAND_FOLDER.'/');
		
	}
	
	function load_dependencies() {
	
		// Load global libraries												
		require_once (dirname (__FILE__) . '/inc/core.php');
		require_once (dirname (__FILE__) . '/inc/db.connect.php');

		// We didn't need all stuff during a AJAX operation
		if ( defined('DOING_AJAX') )
			require_once (dirname (__FILE__) . '/admin/ajax.php');
		else {
			// Load backend libraries
			if ( is_admin() ) {
				require_once (dirname (__FILE__) . '/admin/admin.php');
				//require_once (dirname (__FILE__) . '/admin/media_upload.php');
				$this->grandAdminPanel = new grandAdminPanel();

			// Load frontend libraries							
			} else {

			}
		}
	}
	
	function load_textdomain() {
		
		load_plugin_textdomain('gmLang', false, GRAND_FOLDER.'/languages/');

	}
	
	function load_scripts() {
/*
		// Let's override WP's bundled swfobject, cause as of WP 2.9, it's still using 2.1 
		wp_deregister_script('swfobject');
		// and register our own.
		wp_register_script('swfobject', FLAG_URLPATH .'admin/js/swfobject.js', array(), '2.2');
		wp_enqueue_script('swfobject');

		wp_register_script('swfaddress', FLAG_URLPATH .'admin/js/swfaddress.js', array(), '2.4');
		wp_enqueue_script('swfaddress');
*/
	}

	function load_styles() {

	}	

	function load_options() {
		// Load the options
		$this->options = get_option('grand_options');
	}

	function activate() {
		include_once (dirname (__FILE__) . '/setup.php');
		// check for tables
		grand_install();
	}
	
	function deactivate() {
		// remove & reset the init check option
		delete_option( 'grand_init_check' );
	}

	function uninstall() {
	  	include_once (dirname (__FILE__) . '/setup.php');
	    grand_uninstall();
	}
/*
	// PLUGIN MESSAGE ON PLUGINS PAGE
	function check_message_version($file)
	{
		static $this_plugin;
		global $wp_version;
		if (!$this_plugin) $this_plugin = GRAND_FOLDER;

		if ($file == $this_plugin ){
			$checkfile = "http://codeasily.com/grand-flam.chk";

			$message = wp_remote_fopen($checkfile);

			if($message)
			{
				preg_match( '|grand'.str_replace('.','',GRAND_VERSION).':(.*)$|mi', $message, $theMessage );
				$columns = 5;
				if ( !empty( $theMessage ) )
				{
					$theMessage = trim($theMessage[1]);
					echo '<td colspan="'.$columns.'" class="plugin-update" style="line-height:1.2em; font-size:11px; padding:1px;"><div id="flag-update-msg" style="padding-bottom:1px;" >'.$theMessage.'</div></td>';
				} else {
					return;
				}
			}
		}
	}
*/
	function add_plugin_links($links, $file) {
		
		if ( $file == plugin_basename(__FILE__) ) {
			$links[] = '<a href="admin.php?page=grand-media">' . __('Overview', 'gmLang') . '</a>';
			$links[] = '<a href="#">' . __('Get help', 'gmLang') . '</a>';
			$links[] = '<a href="#">' . __('Contribute', 'gmLang') . '</a>';
			$links[] = '<a href="#">' . __('Donate', 'gmLang') . '</a>';
		}
		return $links;
	}
    
}
	// Let's start the holy plugin
	global $grandLoad;
	$grandLoad = new grandLoad();

}
?>
